package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * @author 李治
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
public class AlarmInfoDetailDaoImpl implements AlarmInfoDetailDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertAlarmInfoDetail(AlarmInfoDetail alarmInfoDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, alarmInfoDetail);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AlarmInfoDetail> getAlarmInfoDetailsByAlarmInfoId(
			BigInteger alarmInfoId, Integer pageNo) {
		Query query = _em.createNativeQuery(
				"select t.* from ALARMINFODETAIL t where t.alarminfoid=?1",
				AlarmInfoDetail.class);
		query.setParameter(1, alarmInfoId);
		int maxResult = Config.PAGE_SIZE;
		int start = maxResult * pageNo;
		query.setFirstResult(start);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AlarmInfoDetail> getAllAlarmInfoDetailsByAlarmInfoId(
			BigInteger alarmInfoId) {
		Query query = _em.createNativeQuery(
				"select t.* from ALARMINFODETAIL t where t.alarminfoid=?1",
				AlarmInfoDetail.class);
		query.setParameter(1, alarmInfoId);
		return query.getResultList();
	}

	@Override
	public Integer getAlarmInfoDetailsCountByAlarmInfoId(BigInteger alarmInfoId) {
		Query query = _em
				.createNativeQuery("select nvl(count(*),0) from ALARMINFODETAIL t where t.alarminfoid=?1");
		query.setParameter(1, alarmInfoId);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public AlarmInfoDetail getAlarmInfoDetailById(
			AlarmInfoDetail alarmInfoDetail) {
		return _em.find(AlarmInfoDetail.class, alarmInfoDetail);
	}

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeAlarmInfoDetailByTranDate(String issueCompanyNo,
			Date tranDate) {
		String sql = "delete alarminfodetail t where exists("
				+ " select a.alarminfoid from alarminfo a"
				+ " where t.alarminfoid = a.alarminfoid"
				+ " and a.issuecompanyno = ?1 and t.trandate = ?2)";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);

		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getAlarmInfoIdFromDetailByTranDate(
			String issueCompanyNo, Date tranDate) {
		String sql = "select distinct(to_char(t.alarminfoid)) from alarminfodetail t,alarminfo a"
				+ " where t.alarminfoid = a.alarminfoid"
				+ " and a.issuecompanyno = ?1" + " and t.trandate = ?2";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		List<String> list = query.getResultList();
		// for(int i=0;i<list.size();i++){
		// System.out.println(list.get(i));
		// }
		return list;
	}
}
